package mk.jug.academy.controller;

import mk.jug.academy.model.Event;
import mk.jug.academy.model.Page;
import mk.jug.academy.service.EventService;
import mk.jug.academy.util.Utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * View layer coordinator for the {@link Event} functionality in the backend.
 *
 * @author Ice.Penov
 */
@Controller
public class EventAdminController extends BaseController {
		
	//TODO: Externalize this.
	public static final int ITEMS_PER_PAGE = 8;
	
	private static final String CREATE_EVENT = "admin.events.create";
	private static final String LIST_EVENTS = "admin.events.list";
	private static final String EDIT_EVENT = "admin.events.edit";
	
	@Autowired private EventService eventService;
	
	@RequestMapping("/admin/events/create.do")
	public String create(final ModelMap map) {
		return new ControllerContext(map) {
			public String execute() {
				Event event = new Event();
				map.addAttribute("event", event);
				return CREATE_EVENT;
			}
		}.result();
	}
	
	@RequestMapping("/admin/events/delete.do")
	public String delete(final ModelMap map, final @RequestParam("id") Long id) {
		return new ControllerContext(map) {
			public String execute() {
				Event event = eventService.load(id);
				eventService.removeEvent(event);
				return "redirect:/admin/events/list.do";
			}
		}.result();
	}
	
	@RequestMapping("/admin/events/list.do")
	public String list(final ModelMap map, final @RequestParam(value = "page", required = false) Integer p) {
		return new ControllerContext(map) {
			public String execute() {
				Integer page = p;
				if (page == null) {
					page = 1;
				}
				Page<Event> pagedEvents = eventService.list(page, ITEMS_PER_PAGE);
				map.addAttribute("events", pagedEvents.getItems());
				map.addAttribute("pages", Utils.getPageNumber(ITEMS_PER_PAGE, pagedEvents.getTotalCount()));
				map.addAttribute("page", page);
				return LIST_EVENTS;
			}
		}.result();
	}
	
	@RequestMapping(value = "/admin/events/createEvent.do", method = RequestMethod.POST)
	public String processEventCreation(final ModelMap map, final @ModelAttribute("event") Event event) {
		return new ControllerContext(map) {
			public String execute() {
				eventService.createEvent(event);				
				return "redirect:/admin/events/list.do";
			}
		}.result();
	}
	
	@RequestMapping(value = "/admin/events/edit.do")
	public String edit(final ModelMap map, final @RequestParam("id") Long id) {
		return new ControllerContext(map) {
			public String execute() {
				Event event = eventService.load(id);
				map.addAttribute("event", event);
				return EDIT_EVENT;
			}
		}.result();
	}
	
	@RequestMapping(value = "/admin/events/saveEvent.do", method = RequestMethod.POST)
	public String saveEvent(final ModelMap map, final @ModelAttribute("event") Event changed, final @RequestParam("id") Long id) {
		return new ControllerContext(map) {
			public String execute() {
				Event event = eventService.load(id);
				event.setMessage(changed.getMessage());
				eventService.saveChanges(event);
				return "redirect:/admin/events/list.do";
			}
		}.result();
	}
	
}